# -*- coding:utf-8 -*-
# @FileName :bot_player.py
# @Created  :2024/9/29
# @Author   :天空之城
import random

from game.actions import PokerActions
from game.player.player_base import Player


class BotPlayer(Player):
    def __init__(self, id, name, chips):
        super().__init__(id, name, chips)
        self.is_human = False

    def get_action(self, highest_bet):
        """
        根据当前下注情况和剩余筹码进行决策
        :param highest_bet: 当前最高的下注金额
        :return: 返回机器人的决定 ('弃牌'/'跟注'/'加注'/'全押'/'过牌')根据概率来
        """
        actions = []
        if highest_bet == 0:
            actions = [PokerActions.CHECK, PokerActions.RAISE, PokerActions.ALL_IN]
            probabilities = [0.5, 0.3, 0.1]  # 过牌和加注各50%的概率
        else:
            if highest_bet <= self.chips:
                actions = [PokerActions.CALL, PokerActions.RAISE, PokerActions.ALL_IN]
                probabilities = [0.4, 0.4, 0.1]  # 跟注和加注各40%的概率，全押20%的概率
            else:
                actions = [PokerActions.ALL_IN]
                probabilities = [0.9]  # 只能全押

        actions.append(PokerActions.FOLD)
        probabilities.append(0.1)  # 弃牌10%的概率

        decision = random.choices(actions, weights=probabilities, k=1)[0]

        if decision == PokerActions.FOLD:
            self.fold()
            return PokerActions.FOLD
        elif decision == PokerActions.CALL:
            self.call(highest_bet)
            return PokerActions.CALL
        elif decision == PokerActions.RAISE:
            raise_amount = self.get_raise_amount(highest_bet)
            self.raise_bet(raise_amount)
            return PokerActions.RAISE
        elif decision == PokerActions.ALL_IN:
            self.all_in()
            return PokerActions.ALL_IN
        elif decision == PokerActions.CHECK:
            self.has_acted = True
            return PokerActions.CHECK

    def get_raise_amount(self, highest_bet):
        """
        获取机器人的加注金额
        :param highest_bet: 当前最高的下注金额
        :return: 加注金额
        """
        min_raise = highest_bet + 1
        if self.chips > min_raise:
            max_raise = max(self.chips, highest_bet * 2)
            return random.randint(min_raise, max_raise)
        else:
            return self.chips

    def execute_action(self, action_code, highest_bet):
        """
        执行玩家的动作
        :param action_code: 动作代码
        :param highest_bet: 当前最高的下注金额
        """
        action_name = PokerActions.ACTIONS.get(action_code)
        if action_name is None:
            print(f"{self.name} 选择了看牌")
            return

        action_method = getattr(self, action_code)
        print('action_method', action_method)

        try:
            if action_code == PokerActions.CALL:
                action_method(highest_bet)
            elif action_code == PokerActions.RAISE:
                lower_bound = highest_bet + 10
                upper_bound = max(highest_bet + 10, self.chips - 10)
                amount = random.randint(lower_bound, upper_bound)
                self.raise_bet(amount)
            else:
                action_method()
        except Exception as e:
            print(f"{self.name} 执行动作时发生错误: {str(e)}")
